package mulMan.gui.controller;

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.List;
import java.util.Observable;

import javax.swing.JMenu;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import mulMan.domain.AudioFile;
import mulMan.domain.AudioFileFactory;
import mulMan.domain.Playlist;
import mulMan.domain.PlaylistFactory;
import mulMan.gui.view.AddAudioFileView;
import mulMan.gui.view.AddPlaylistView;
import mulMan.gui.view.AudioFilesView;
import mulMan.gui.view.ModifyPlaylistView;
import mulMan.gui.view.PlaylistMenuView;
import mulMan.gui.view.PlaylistsFullView;
import mulMan.gui.view.PlaylistsPopupView;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class PlaylistsController extends Observable implements ApplicationContextAware{
	
	private List<Playlist> playlists;
	private Container view;
	private ApplicationContext applicationContext;
	private AudioFilesController audioFilesController;
	

	public Container getView() {
		return view;
	}
	public void setView(Container view) {
		this.view = view;
	}

	public void setAudioFilesController(AudioFilesController audioFilesController) {
		this.audioFilesController = audioFilesController;
	}
	
	
	public PlaylistsController(List<Playlist> playlists, Container view) {
		this.playlists = playlists;
		this.view = view;
		
		
		if (view instanceof PlaylistsFullView) {
			((PlaylistsFullView) view).getListOfPlaylists().setSelectedIndex(0);
			((PlaylistsFullView) view).addListPlaylistsListener(new ListPlaylistsListener());
			((PlaylistsFullView) view).addAddNewAudioFileListener(new AddNewAudioFileListener());
			((PlaylistsFullView) view).addAddNewPlaylistListener(new AddNewPlaylistListener());
			((PlaylistsFullView) view).addExitListener(new ExitListener());
			((PlaylistsFullView) view).addListPlaylistsMouseListener(new ListPlaylistsMouseListener());
			((PlaylistsFullView) view).adddeletePlaylistListener(new DeletePlaylistListener());
		} else if (view instanceof PlaylistsPopupView) {
			((PlaylistsPopupView) view).addshowPlaylistsMenuListListener(new ShowPlaylistsMenuListListener());
		}
	}
	
	public void addAudioFilesView() {
		Playlist selectedPlaylist = playlists.get(((PlaylistsFullView) view).getListOfPlaylists().getSelectedIndex());
		
		AudioFilesView audioFilesView = new AudioFilesView(selectedPlaylist.getListOfAudioFiles());
		applicationContext.getBean("audioFilesController", selectedPlaylist.getListOfAudioFiles(), audioFilesView);
		
		((PlaylistsFullView) view).setAudioFilesView(audioFilesView);
		
	}
	
	
	//ACTION LISTENERS
	
	class ListPlaylistsMouseListener implements MouseListener {

		public void mouseClicked(MouseEvent e) {
			if((e.getClickCount() == 2) && (playlists.get(((PlaylistsFullView) view).getListOfPlaylists().getSelectedIndex()).getMain() != 1)){
				
	            new PlaylistController(playlists.get(((PlaylistsFullView) view).getListOfPlaylists().getSelectedIndex()), 
	            		new ModifyPlaylistView(playlists.get(((PlaylistsFullView) view).getListOfPlaylists().getSelectedIndex())));
	            
			}
		}

		public void mouseEntered(MouseEvent e) {}
		public void mouseExited(MouseEvent e) {}
		public void mousePressed(MouseEvent e) {
			if (e.isPopupTrigger()) {
				showPopup(e);
			}
		}
		public void mouseReleased(MouseEvent e) {
			if ((e.isPopupTrigger()) && (playlists.get(((PlaylistsFullView) view).getListOfPlaylists().locationToIndex(e.getPoint())).getMain() != 1)) {
				showPopup(e);
			}
		}

		private void showPopup(MouseEvent e) {
			((PlaylistsFullView) view).getListOfPlaylists().setSelectedIndex(((PlaylistsFullView) view).getListOfPlaylists().locationToIndex(e.getPoint()));
			((PlaylistsFullView) view).getPopupMenu().show(((PlaylistsFullView) view).getListOfPlaylists(), e.getX(), e.getY());
		}
    }
	
	
	class AddNewAudioFileListener implements ActionListener {

		public void actionPerformed(ActionEvent arg0) {
			AudioFileFactory audioFileFacory = (AudioFileFactory) applicationContext.getBean("audioFileFactory");
			AudioFile audioFile;
			try {
				audioFile = audioFileFacory.addAudioFileToPlaylist();
				new AudioFileController(audioFile, new AddAudioFileView(audioFile));
			} catch (Exception e) {
				e.printStackTrace();
			}
			
		}
		
	}
	
	
	class DeletePlaylistListener implements ActionListener {

		public void actionPerformed(ActionEvent arg0) {
			playlists.remove(((PlaylistsFullView) view).getListOfPlaylists().getSelectedIndex());
			((PlaylistsFullView) view).getListOfPlaylists().setSelectedIndex(0);
			addAudioFilesView();
		}
		
	}
	
	class AddNewPlaylistListener implements ActionListener {

		public void actionPerformed(ActionEvent arg0) {
			PlaylistFactory playlistFacory = (PlaylistFactory) applicationContext.getBean("playlistFactory");
			Playlist Playlist = playlistFacory.addPlaylistToListOfPlaylist();
			
			new PlaylistController(Playlist, new AddPlaylistView(Playlist));
		}
		
	}
	
	class ExitListener implements ActionListener {

		public void actionPerformed(ActionEvent arg0) {
			System.exit(0);
		}
		
	}
	
	class ListPlaylistsListener implements ListSelectionListener {

		public void valueChanged(ListSelectionEvent arg0) {
			if (arg0.getValueIsAdjusting()) {
	            addAudioFilesView();
	        }
		}

    }
	
	
	
	class ShowPlaylistsMenuListListener implements ChangeListener {
		
		public void stateChanged(ChangeEvent e) {
			if (((JMenu) view).isSelected()) {

				((JMenu) view).removeAll();
				
				for (Playlist iterator : playlists) {
					if (!iterator.getName().equals("Music")) {
						PlaylistController playlistController = new PlaylistController(iterator ,((JMenu) view).add(new PlaylistMenuView(iterator)));
						playlistController.setAudioFilesController(audioFilesController);
					}
				}
			}
		}
		
	}
	
	
	

	public void setApplicationContext(ApplicationContext arg0)
			throws BeansException {
		this.applicationContext = arg0;
	}
	
	
}
